package common.resource.loaders
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	import flash.utils.Dictionary;
	import flash.utils.Endian;
	
	import module.avatar.animator.FrameInfoVO;

	public class AnimatorLoader extends EventDispatcher
	{
		private var _loader:URLLoader;
		private var _imgLoader:Loader;
		private var _path:String;
		private var _animId:String;
		private var _textureWidth:int;
		private var _textureHeight:int;
		private var _frameNum:int;
		private var _frameDic:Array;
		private var _frame:int;
		public var bmpDic:Dictionary;
		
		public function AnimatorLoader(path:String, animId:String)
		{
			_path = path;
			_animId = animId;
			
			_frameDic = new Array();
		}
		
		public function load():void
		{
			_loader = new URLLoader();
			_loader.dataFormat = URLLoaderDataFormat.BINARY;
			_loader.addEventListener(Event.COMPLETE, onTbeload);
			_loader.load(new URLRequest(_path+_animId+".tbe"));
		}
		
		private function onTbeload(e:Event):void
		{
			var content:ByteArray = e.target.data;
			content.endian = Endian.LITTLE_ENDIAN;
			
			_textureWidth 	= content.readShort();
			_textureHeight 	= content.readShort();
			_frameNum 		= content.readShort();
			
			var frameVO:FrameInfoVO = new FrameInfoVO();
			for(var i:int = 0; i < _frameNum; i++)
			{
				frameVO.frame 	= content.readShort();
				frameVO.offsetX = content.readShort();
				frameVO.offsetY = content.readShort();
				
				frameVO.centerOffsetX 	= content.readByte();
				frameVO.centerOffsetY 	= content.readByte();
				frameVO.rect.left 	= content.readShort();
				frameVO.rect.top	= content.readShort();
				frameVO.rect.right = content.readShort();
				frameVO.rect.bottom = content.readShort();
				
				_frameDic.push(frameVO.clone());
			}
			_imgLoader = new Loader();
			_imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onImageLoad);
			_imgLoader.load(new URLRequest(_path + _animId + ".png"));
		}
		
		private function onImageLoad(e:Event):void
		{
			bmpDic = new Dictionary();
			
			var bmp:Bitmap = LoaderInfo(e.target).content as Bitmap;
			var px:int = 0;
			var py:int = 0;
			for(var i:int = 0; i < _frameNum; i++)
			{
				var frameInfo:FrameInfoVO = _frameDic[i];
				var bmd:BitmapData = new BitmapData(frameInfo.rect.width, frameInfo.rect.height, true, 0x00);
				bmd.copyPixels(bmp.bitmapData, frameInfo.rect, new Point(0,0));
				var dp:Bitmap = new Bitmap(bmd);
				dp.x = frameInfo.centerOffsetX;
				dp.y = frameInfo.centerOffsetY;
				bmpDic[i] = dp;
				
//				dp.x = px;
//				dp.y = py;
//				px += dp.width;
//				CopySword.stageA.addChild(dp);
			}
			dispatchEvent(new Event(Event.COMPLETE));
		}
		//
	}
}